package com.wss.module.user.ui.account;


import com.wss.common.base.BaseActionBarAbility;

import com.wss.common.bean.Event;
import com.wss.common.constants.ARouterConfig;
import com.wss.common.constants.EventAction;
import com.wss.common.manage.AbilityToAbility;
import com.wss.common.widget.ObserverButton;
import com.wss.module.user.ResourceTable;
import com.wss.module.user.ui.account.mvp.LoginPresenter;
import com.wss.module.user.ui.account.mvp.contract.LoginContract;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;

/**
 * Describe：登录
 * Created by 吴天强 on 2018/11/13.
 */
public class LoginActivity extends BaseActionBarAbility<LoginPresenter>
        implements LoginContract.View,
        Component.ClickedListener {

    TextField edtName;
    TextField edtPwd;
    ObserverButton obLogin;
    Text obRegister;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        initView();
    }

    @Override
    protected int getLayoutId() {
        return ResourceTable.Layout_user_layout_login;
    }


    protected void initView() {
        setCenterText("登录");

        edtName = (TextField) findComponentById(ResourceTable.Id_edt_name);
        edtPwd = (TextField) findComponentById(ResourceTable.Id_edt_pwd);
        obLogin = (ObserverButton) findComponentById(ResourceTable.Id_ob_login);
        obRegister = (Text) findComponentById(ResourceTable.Id_ob_register);

        obLogin.setClickedListener(this);
        obRegister.setClickedListener(this);

        obLogin.observer(edtName, edtPwd);
        if (!edtName.getText().isEmpty() && !edtPwd.getText().isEmpty()) {
            obLogin.setPressEnable(true);
        }
    }


    @Override
    protected LoginPresenter createPresenter() {
        return new LoginPresenter();
    }

    @Override
    public void onEventBus(Event event) {
        super.onEventBus(event);
        if (event.getAction()==EventAction.EVENT_REGISTER_SUCCESS) {
            terminateAbility();
        }
    }

    @Override
    protected boolean registerEventBus() {
        return true;
    }

    @Override
    public void onClick(Component component) {
        int i = component.getId();
        if (i == ResourceTable.Id_ob_login) {
            getPresenter().login(edtName.getText().toString().trim(),
                    edtPwd.getText().toString().trim());
        } else if (i == ResourceTable.Id_ob_register) {
            AbilityToAbility.toAbility(context, RegisterActivity.class);
        }
    }
}
